﻿using System.Collections.Generic;
using Web7.Extensions;
using System;
using Web7.Core.Apps;
using System.Linq;

namespace Web7.Core.ViewResolvers
{
    /// <summary>
    /// 基本视图解析器
    /// </summary>
    public class Web7ViewResolver : IViewResolver
    {
        private readonly Web7Context _context;

        public Web7ViewResolver(Web7Context context)
        {
            this._context = context;
        }

        #region IViewResolver Members

        /// <summary>
        /// 视图解析
        /// </summary>
        /// <example>
        /// <para>如视图主目录是~/views，视图名是default，则会生成：
        /// <para>~/views/default</para>
        /// <para>如视图主目录是~/views，视图名是default/mydefault,则会生成：</para>
        /// <para>~/views/default</para>
        /// <para>~/views/default/mydefault</para>
        /// <para>如视图主目录是~/views，视图名是/default/mydefault,则会生成：</para>
        /// <para>~/views/default</para>
        /// <para>~/views/default/mydefault</para>
        /// <para>如视图主目录是~/views/myviews，视图名是default/mydefault,则会生成：</para>
        /// <para>~/views/myviews/default</para>
        /// <para>~/views/myviews/default/mydefault</para>
        /// <para>如视图主目录是~/views/myviews，视图名是/default/mydefault,则会生成：</para>
        /// <para>~/views/myviews/default</para>
        /// <para>~/views/myviews/default/mydefault</para>
        /// </example>
        /// <remarks>
        /// 尽管视图名称支持多级目录形式，但建议使用单目录名的形式，如：defualt
        /// </remarks>
        /// <param name="viewResolverContext">视图解析器上下文</param>
        /// <param name="viewRootPaths">视图路径集</param>
        public void Resolve(ViewResolverContext viewResolverContext, IList<string> viewRootPaths)
        {
            if (viewResolverContext == null)
                throw new ArgumentNullException("viewResolverContext");
            if (viewRootPaths == null)
                throw new ArgumentNullException("viewRootPaths");
            if (viewResolverContext.ViewRoot.IsNullOrWhiteSpace())
                return;

            if (AppConstiant.AppsUnderRoot.Count(m=>m.ToLower() == viewResolverContext.ViewRoot.ToLower())>0)
            {
                viewRootPaths.Add(viewResolverContext.ViewRoot.EnsureVirtualDirectory());
            }
            else
            {
                ResolveViewRootPaths(viewResolverContext, _context.Site.AppsViewsRoot, viewRootPaths);
            }
        }

        private void ResolveViewRootPaths(ViewResolverContext viewResolverContext, string viewsRoot, IList<string> viewRootPaths)
        {
            string viewsPath = viewsRoot.EnsureVirtualDirectory();
            string[] viewNames = viewResolverContext.ViewRoot.EnsureSubFolder().Split('/');
            string viewRootPath =string.Format("{0}/{1}", viewsPath, viewNames[0]);

            viewRootPaths.Add(viewRootPath);

            if (viewNames.Length > 1)
                for (int i = 1; i < viewNames.Length; i++)
                    viewRootPaths.Add(viewRootPath = string.Format("{0}/{1}", viewRootPath, viewNames[i]));
        }

        #endregion
    }
}
